//! @Author: DengLibin 榆霖
//! @Date:  2022-04-20 16:51:37
//! @Description: pin 钉住

use std::{marker::PhantomPinned, pin::Pin};

#[derive(Debug)]
struct User {
    name: String, 
    ptr: *const String,
    _marker: PhantomPinned
}

impl User {
    fn new(name: &str) -> Self {
        let mut u = User{
            name: String::from(name),
            ptr: std::ptr::null(),
            _marker: PhantomPinned

        };

        u.ptr = &u.name;
        u
    }
}

#[test]
pub fn run(){
    let u = User::new("Peter");
    let p: *const User = &u;
    let p2: *const *const String = &(u.ptr);
    println!("{:?}", u);
    println!("{:?}", p);
    println!("{:?}", p2);

    let mut u1 = User::new("u1");
    let mut u1 = unsafe {
        Pin::new_unchecked(&mut u1)
    };
    let mut u2 = User::new("u2");

    let mut u2 = unsafe {
        Pin::new_unchecked(&mut u2)
    };


    println!("a:{}, b:{}", u1.name, u2.name);
    //std::mem::swap(&mut u1, &mut u2);
    //Pig固定的对象 不能移动
    //std::mem::swap(u1.get_mut(), u2.get_mut());
    println!("a:{}, b:{}", u1.name, u2.name);

    unsafe{
        println!("a:{}, b:{}", *u1.ptr, *u2.ptr);
    }
    

}